package com.atguigu.gmall.product.rest;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.vo.SaveAttrInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description 平台属性
 * @create 2023-08-15 9:54
 */
@RequestMapping("/admin/product")
@RestController
public class BaseAttrController {


    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;
    /**
     * 查询分类下定义的所有平台属性名和值
     * @return
     */
    @GetMapping("/attrInfoList/{c1id}/{c2id}/{c3id}")
    public Result attrInfoList(@PathVariable("c1id") Long c1id,
                               @PathVariable("c2id") Long c2id,
                               @PathVariable("c3id") Long c3id){

        List<BaseAttrInfo> infos = baseAttrInfoService
                .attrInfoList(c1id,c2id,c3id);
        return Result.ok(infos);
    }

    /**
     * 保存平台属性/ 修改二合一
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody SaveAttrInfoVo vo){
        if (vo.getId()==null) {
            baseAttrInfoService.saveAttrInfo(vo);
        }else {
            baseAttrInfoService.updateAttrInfo(vo);
        }


        return Result.ok();
    }

    /**
     * 获取某个平台属性的所有值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){

        List<BaseAttrValue> list = baseAttrValueService.lambdaQuery()
                .eq(BaseAttrValue::getAttrId, attrId)
                .list();
        return Result.ok(list);
    }
}
